package dlally.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

import dlally.model.DecisionLinesEvent;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import dlally.controller.ChoiceController;
import dlally.controller.EdgeController;



import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

@SuppressWarnings("serial")
/**
 * A GUI created with the help of WindowBuilder for the user to interact with the DecisionLines game
 * @author Douglas Lally (dlally@wpi.edu)
 *
 */
public class GameFieldGUI extends JFrame {

	/**
	 * Each GameFieldGUI will need to know its associated model, and have a contentPane, an array of JTextFields to display the choices, a gameField to draw graphics on, and for
	 * the user to interact with, a generic label to display instructions/information to the user, and a boolean to keep track of if a user is allowed to add edges by clicking
	 */
	DecisionLinesEvent model;

	private JPanel contentPane;
	JTextField[] choices;
	GameFieldPanel gameField;
	JLabel lblDecisionLines;
	boolean isMouseListenerActive = false;




	/**
	 * Create the frame.
	 */
	public GameFieldGUI(DecisionLinesEvent m) {
		setResizable(false);
		this.model = m;
		setTitle("Decision Lines");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 825, 500);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		//JPanel gameField = new JPanel();
		gameField = new GameFieldPanel(model);
		gameField.addMouseListener(new MouseListener(){
			@Override
			public void mouseClicked(MouseEvent arg0) {
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
			}

			@Override
			public void mouseExited(MouseEvent arg0) {				
			}

			@Override
			public void mousePressed(MouseEvent arg0) {		
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				if(isMouseListenerActive)
					new EdgeController(GameFieldGUI.this, model, arg0).process();
				
			}});
		
		JPanel textPanel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) textPanel.getLayout();
		flowLayout.setHgap(7);
		flowLayout.setAlignment(FlowLayout.LEFT);
		flowLayout.setAlignOnBaseline(true);
		
		lblDecisionLines = new JLabel("Please enter valid choices in all boxes below");
		lblDecisionLines.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblDecisionLines.setHorizontalAlignment(SwingConstants.CENTER);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addComponent(gameField, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 809, Short.MAX_VALUE)
						.addComponent(lblDecisionLines, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 809, Short.MAX_VALUE)
						.addComponent(textPanel, GroupLayout.DEFAULT_SIZE, 809, Short.MAX_VALUE))
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblDecisionLines, GroupLayout.DEFAULT_SIZE, 18, Short.MAX_VALUE)
					.addGap(23)
					.addComponent(textPanel, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(gameField, GroupLayout.PREFERRED_SIZE, 323, GroupLayout.PREFERRED_SIZE)
					.addGap(43))
		);
		choices = new JTextField[model.getNumPlayers()];
		for(int i = 0; i < model.getNumPlayers(); i++){
			final int j = i;
			choices[i] = new JTextField();
			choices[i].setColumns(8);
			choices[i].setEditable(true);
			choices[i].setVisible(true);
			textPanel.add(choices[i]);
			choices[i].addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent ae) {
					new ChoiceController(GameFieldGUI.this, model, j).process();
					
				}
		});
		contentPane.setLayout(gl_contentPane);
	}
	}
	/**
	 * @return the array of JTextFields containing the choices
	 */
	public JTextField[] getChoices(){
		return choices;
	}
	
	/**
	 * @param i index of the choice
	 * @return the JTextField at that index in the choices array
	 */
	public JTextField getChoice(int i){
		return choices[i];
	}
	/**
	 * @return the gameField
	 */
	public GameFieldPanel getPanel(){
		return gameField;
	}
	/**
	 * @return the label used to display information to the user
	 */
	public JLabel getLabel(){
		return lblDecisionLines;
	}
	/**
	 * Prevents the user from adding any additional edges
	 */
	public void disableMouseListener(){
		isMouseListenerActive = false;
	}
	/**
	 * Allows the user to add edges
	 */
	public void enableMouseListener(){
		isMouseListenerActive = true;
	}
	/**
	 * 
	 * @return true if the user can add edges, false otherwise
	 */
	public boolean getIsMouseListenerActive(){
		return isMouseListenerActive;
	}
	/**
	 * Prevents the JTextFields from being edited any further
	 */
	public void lockTextFields(){
		for(int i = 0; i < choices.length; i++){
			choices[i].setEditable(false);
		}
	}
	
}
